import hashlib

class Person:
    def __init__(self, PID, f_Name, l_Name, UserName, Password = ' ', Email = ' ', Admin = 'False', BankAccount =""):
        self.PID = PID
        self.f_Name = f_Name
        self.l_Name = l_Name
        self.UserName = UserName
        self.Email = Email
        self.BankAccount = BankAccount
        self.Password = Password
        self.Admin = bool('True' == Admin)
        
    def SetPassWord(self, NewPassword):
        self.Password = hashlib.sha224(str(NewPassword)).hexdigest()
    
    def CheckPassword(self, Password):
        return bool(str(self.Password) == str(hashlib.sha224(str(Password)).hexdigest()))
        
    
        
class People_Data:
    def __init__(self, SQLConn, dBName = "People_Details"):
        self.PeopleDataTableName = dBName
        self.SQLConn = SQLConn
        
    def AddPerson(self, First_Name, Last_Name, UserName,
                  Password = ' ', Email = ' ', Admin = 'False', BankAccount = ""):
        self.SQLConn.SendCommand("""
         INSERT INTO %s (f_name, l_name, Usr_name, Password, Email, Admin, BankAccount)
         VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')
       """ %(self.PeopleDataTableName, First_Name, Last_Name, UserName, str(hashlib.sha224(str(Password)).hexdigest()), Email, Admin, BankAccount))
    
    def RemovePerson(self, PID):
        self.SQLConn.SendCommand("""    
         DELETE FROM %s where Peo_id=%d
         """ % (self.PeopleDataTableName, PID))
        
    def GetAllPeopleDict(self):
        self.SQLConn.SendCommand("""SELECT Peo_id, f_name, l_name, Usr_name, Admin, Password, Email, BankAccount FROM %s"""%(self.PeopleDataTableName))
        rows = self.SQLConn.FetchAll ()
        People = {}
        for person in rows:
            People[int(person[0])] = Person(int(person[0]), person[1], person[2],
                                            person[3], Admin = person[4], Password = person[5], Email = person[6], BankAccount = person[7])
        return People
    
    def UpdatePerson(self, Person):
       self.SQLConn.SendCommand("""UPDATE %s
                                SET f_name = '%s',
                                l_name = '%s',
                                Usr_name = '%s',
                                Admin = '%s',
                                Password = '%s',
                                Email = '%s',
                                BankAccount = '%s'
                                WHERE Peo_id = %d
                                """ % (self.PeopleDataTableName, Person.f_Name, Person.l_Name, Person.UserName,
                                       Person.Admin, Person.Password, Person.Email, Person.BankAccount, Person.PID))
        
        
    def GetPerson(self, f_name='', l_name = '', PID = -1, UserName = '', Admin = ''):
        First = True
        str = """WHERE """
        if Admin != '':
            if First:
                First = False
                str = str + """Admin = '%s' """ % Admin
            else:
                str = str + """AND Admin = '%s' """ % Admin
        if PID != -1:
            if First:
                First = False
                str = str + """Peo_id = '%d' """ % PID
            else:
                str = str + """AND Peo_id = '%d' """ % PID
        if f_name != '':
            if First:
                First = False
                str = str + """f_name = '%s' """ % f_name
            else:
                str = str + """AND f_name = '%s' """ % f_name
        if l_name != '':
            if First:
                First = False
                str = str + """l_name = '%s' """ % l_name
            else:
                str = str + """AND l_name = '%s' """ % l_name
        if UserName != '':
            if First:
                First = False
                str = str + """Usr_name = '%s' """ % UserName
            else:
                str = str + """AND Usr_name = '%s' """ % UserName
        
        self.SQLConn.SendCommand("""SELECT Peo_id, f_name, l_name, Usr_name, Password, Email, Admin, BankAccount FROM %s """%(self.PeopleDataTableName) + str)
        Data = self.SQLConn.FetchAll ()
        Res = []
        for person in Data:
            Res.append(Person(int(person[0]), person[1], person[2], person[3], Password = person[4], Email = person[5], Admin = person[6], BankAccount = person[7] ))
                    
        return Res

def GenerateTables(SQL):
    print """Generating "People_Details Table"""
    SQL.SendCommand ("DROP TABLE IF EXISTS People_Details")
    SQL.SendCommand ("""CREATE TABLE People_Details(
                    Peo_id int unsigned not null auto_increment primary key,
                    f_name varchar(20),
                    l_name varchar(20),
                    Usr_name varchar(20),
                    Password varchar(2000),
                    Email varchar(200),
                    BankAccount varchar(200),
                    Admin VARCHAR(60) Default 'False'
                    )
                    """)